package com.boolib.animate.framework.web;

import com.boolib.animate.commons.base.BaseController;
import com.boolib.animate.framework.entity.AnimateChapter;
import com.boolib.animate.framework.entity.FileInfo;
import com.boolib.animate.framework.service.advice.AnimateChapterService;
import com.boolib.animate.framework.service.base.impl.FileInfoService;
import com.boolib.animate.framework.service.extend.impl.UploadServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;

/**
 * @author nier
 * @create 2020-02-20 18:37
 */

@RequestMapping("/api/upload")
@RestController
public class UploadController  {

    @Value("${system.uploadpath}")
    private String uploadPath;

    @Value("${system.domain}")
    private String domain;
    @Autowired
    private FileInfoService fileInfoService;

    @Autowired
    private UploadServiceImpl uploadServiceImpl;

    @PostMapping("animate")
    public Object upload(MultipartFile file) throws IOException, FileUploadException {
       return  fileInfoService.uploadImage(uploadPath,file, FileInfo.FILETYPE_ANIMATE);
    }

    @ResponseStatus(HttpStatus.NO_CONTENT)
    @DeleteMapping("{name}")
    public void delete(@PathVariable("name") String name) throws IOException, FileUploadException {
        FileInfo fileInfo =FileInfo.builder()
                .newName(name).build();
          fileInfoService.delete(fileInfo);

        uploadServiceImpl.removeOne(uploadPath,name);

    }



}
